<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | HTTP请求 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace common;

class Http {
    /**
     * 提交表单
     *
     * @param $url
     * @param $data
     * @param bool $useragent
     * @param bool $opts
     * @return mixed|string
     */
    public static function postForm($url, $data, $useragent = false, $opts = false) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_VERBOSE, 1);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if ($useragent) {
            curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
        }
        if (is_array($opts)) {
            curl_setopt_array($curl, $opts);
        }

        $response = curl_exec($curl);
        if ($response === false) {
            $response = curl_error($curl);
        }

        curl_close($curl);
        return $response;
    }

    /**
     * 以x-www-form-urlencoded提交数据
     *
     * @param $url
     * @param $data
     * @param bool $useragent
     * @param bool $opts
     * @return mixed|string
     */
    public static function postXWwwFormUrlencoded($url, $data, $useragent = false, $opts = false) {
        $data = http_build_query($data);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
        curl_setopt($curl, CURLOPT_VERBOSE, 1);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if ($useragent) {
            curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
        }
        if (is_array($opts)) {
            curl_setopt_array($curl, $opts);
        }

        $response = curl_exec($curl);
        if ($response === false) {
            $response = curl_error($curl);
        }

        curl_close($curl);
        return $response;
    }

    /**
     * 发送postJson请求
     *
     * @param $url
     * @param $data
     * @param bool $useragent
     * @param bool $opts
     * @return mixed|string
     */
    public static function postJson($url, $data, $useragent = false, $opts = false) {
        if (is_array($data) || is_object($data)) {
            $data = json_encode($data);
        }

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_VERBOSE, 1);
        curl_setopt($curl, CURLINFO_HEADER_OUT, false);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data)));
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if ($useragent) {
            curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
        }
        if (is_array($opts)) {
            curl_setopt_array($curl, $opts);
        }

        $response = curl_exec($curl);
        if ($response === false) {
            $response = curl_error($curl);
        }
        curl_close($curl);
        return $response;
    }

    /**
     * 发送get请求
     *
     * @param $url
     * @param bool $useragent
     * @param bool $opts
     * @return mixed|string
     */
    public static function get($url, $useragent = false, $opts = false) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        if ($useragent) {
            curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
        }
        if (is_array($opts)) {
            curl_setopt_array($curl, $opts);
        }

        $response = curl_exec($curl);
        if ($response === false) {
            $response = curl_error($curl);
        }
        curl_close($curl);
        return $response;
    }
}